﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farigola.Engine.SystemData.Entity.Type;
using Farigola.Engine.SystemData.Entity;

namespace Farigola.Engine.SystemData
{
    /// <summary>
    /// Manager of types.
    /// Used in project (active and others).
    /// and also the workspace to manage built-in types.
    /// </summary>
    public class TypeMgr
    {
        /// <summary>
        /// The unique errors manager of the engine application.
        /// Provided to main object in the engine (workspace, project,...).
        /// </summary>
        //private ErrorMgr _errorMgr;

        /// <summary>
        /// Manager of tasks activity in the application (trace)
        /// Manage alos errors occurs in the engine.
        /// </summary>
        private AppActivityMgr _appActivityMgr;

        /// <summary>
        /// List of managed entities types.
        /// </summary>
        private List<EntTypeBase> _listEntType = new List<EntTypeBase>();

        //---------------------------------------------------------------------
        /// <summary>
        /// Contructor.
        /// </summary>
        /// <param name="errorMgr"></param>
        public TypeMgr(AppActivityMgr appActivityMgr)
        {
            _appActivityMgr = appActivityMgr;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// return true if the mgr contains the entity type.
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool contains(EntTypeBase entType)
        {
            if (entType == null)
                return false;
            return _listEntType.Contains(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return the entity type by the name.
        /// return null if not found or if the name is wrong (null or empty).
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public EntTypeBase FindEntTypeByName(string name)
        {
		    if(name == null)
			    return null;
		
		    name = name.Trim();
		
		     foreach(EntTypeBase ent in _listEntType) {
		            if (ent.GetName().Equals(name,StringComparison.OrdinalIgnoreCase)) 
		                return ent;		        
		     }
		     return null;
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// return the entity type by the id.
        /// return null if not found or if the id is wrong (null or empty).
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public EntTypeBase FindEntTypeById(EntityId id)
        {
		    if(id==null)
			    return null;
		
		    if(!id.CheckInternal())
			    return null;
		
		     foreach(EntTypeBase ent in _listEntType) {
		            if (ent.GetId().IdAreEquals(id)) 
		                return ent;
		     }
		     return null;
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Add the entity in the mgr.
        /// check that is not already present
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool Add(EntTypeBase entType)
        {
            if (entType == null)
                return false;

            if (_listEntType.Contains(entType))
                return false;

            _listEntType.Add(entType);
            return true;
        }
    }
}
